package top.csmcool.musicapi.resource;

import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.csmcool.musicapi.client.QQClient;
import top.csmcool.musicapi.config.SystemProperties;
import top.csmcool.musicapi.domain.UserPlatformBindInfo;
import top.csmcool.musicapi.domain.Users;
import top.csmcool.musicapi.enums.PlatformEnum;
import top.csmcool.musicapi.model.api.navidrome.SongCollection;
import top.csmcool.musicapi.model.api.qq.*;
import top.csmcool.musicapi.model.playlist.*;
import top.csmcool.musicapi.util.TreadLocalUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author shizhe
 * @Date 2025/2/7
 */

@Component("qq")
@RequiredArgsConstructor
public class QQResource implements MusicResource{

    private final SystemProperties systemProperties;
    private final QQClient qqClient;

    /**
     * 只获取100条数据
     *
     * @param requestRequest 请求
     * @return list
     */
    @Override
    public List<PlayListMusicInfo> getTracks(TracksRequest requestRequest, Users users, UserPlatformBindInfo one) {
        List<PlayListMusicInfo> result = new LinkedList<>();
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        JSONObject parameter = new JSONObject();
        parameter.set("id", requestRequest.getId());
        List<Song> songlist = qqClient.songlist(parameter);
        // 组装反参
        if(songlist != null) {
            // 获取歌曲链接
            List<PlayListMusicInfo> result1 = getPlayListMusicInfos(songlist, result);
            if (result1 != null) return result1;
        }

        return result;
    }

    @Override
    public List<LyricInfo> getLyric(LyricRequest body, Users users, UserPlatformBindInfo one) {
        List<LyricInfo> lyricInfos = new LinkedList<>();
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        JSONObject parameter = new JSONObject();
        parameter.set("songmid", body.getId());
        String lyric = qqClient.lyric(parameter);
        if(StringUtils.hasText(lyric)) {
            lyricInfos = parseLyric(lyric);
        }
        return lyricInfos;
    }

    @Override
    public List<SongCollection> getPlayList(PlayListRequest body, Users users, UserPlatformBindInfo one) {
        List<SongCollection> songCollections = new LinkedList<>();

        // 获取token
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        JSONObject parameter = new JSONObject();
        parameter.set("id", one.getUsername()); // qq账号
        UserSongList userSongList = qqClient.userSonglist(parameter);
        List<MusicDirectory> musicDirectories = userSongList.getList();
        HostInfo creator = userSongList.getCreator();
        for (MusicDirectory musicDirectory : musicDirectories) {
            SongCollection songCollection = new SongCollection();
            songCollection.setId(String.valueOf(musicDirectory.getDirid()));
            songCollection.setCoverImgUrl(musicDirectory.getDiss_cover());
            songCollection.setName(musicDirectory.getDiss_name());
            songCollection.setSongCount(musicDirectory.getSong_cnt());
            songCollection.setOwnerName(creator.getHostname());
            songCollection.setOwnerId(creator.getHostuin());
            songCollections.add(songCollection);
        }

        return songCollections;
    }

    @Override
    public List<SongCollection> getPublicPlayList(PlayListRequest body, Users users, UserPlatformBindInfo one) {
        List<SongCollection> songCollections = new LinkedList<>();
        // 获取token
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        List<MusicPlayList> musicDirectories = qqClient.recommendPlaylistU(new JSONObject());
        for (MusicPlayList musicDirectory : musicDirectories) {
            SongCollection songCollection = new SongCollection();
            songCollection.setId(String.valueOf(musicDirectory.getContent_id()));
            songCollection.setCoverImgUrl(musicDirectory.getCover());
            songCollection.setName(musicDirectory.getTitle());
            songCollection.setSongCount(0);
            songCollection.setOwnerName(musicDirectory.getUsername());
            songCollection.setOwnerId(String.valueOf(musicDirectory.getCreator()));
            songCollections.add(songCollection);
        }

        return songCollections;
    }

    @Override
    public List<PlayListMusicInfo> search(SearchRequest body, Users users, UserPlatformBindInfo one) {
        List<PlayListMusicInfo> songs = new LinkedList<>();

        // 获取token
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        JSONObject parameter = new JSONObject();
        parameter.set("key", body.getName());
        parameter.set("pageNo", body.getPage() + 1);
        parameter.set("pageSize", body.getSize());
        parameter.set("t", 0);
        List<Song> songList = qqClient.search(parameter);
        List<PlayListMusicInfo> songs1 = getPlayListMusicInfos(songList, songs);
        if (songs1 != null) return songs1;
        return songs;
    }

    @Override
    public SongInfo getSongUrl(SongUrlRequest body, Users users, UserPlatformBindInfo one) {
        SongInfo songInfo = new SongInfo();
        // 获取token
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl() + "/" + PlatformEnum.QQ.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);

        cn.hutool.json.JSONObject parameter = new cn.hutool.json.JSONObject();
        parameter.set("id", body.getId());
        String url = qqClient.songUrl(parameter);
        songInfo.setSongUrl(url);
        return songInfo;
    }

    private List<PlayListMusicInfo> getPlayListMusicInfos(List<Song> songList, List<PlayListMusicInfo> songs) {
        String songIds = songList.stream().map(Song::getSongmid).collect(Collectors.joining(","));
        JSONObject json = new JSONObject();
        json.set("id",songIds);
        JSONObject songsData = qqClient.songUrls(json);
        if(songsData == null) {
            songsData = new JSONObject();
        }
        for (Song song : songList) {
            PlayListMusicInfo playListMusicInfo = new PlayListMusicInfo();
            playListMusicInfo.setId(song.getSongmid());
            playListMusicInfo.setName(song.getSongname());
            String artists = song.getSinger().stream().map(Singer::getName).collect(Collectors.joining("/"));
            playListMusicInfo.setSinger(artists);
            playListMusicInfo.setAlbum(song.getAlbumname());
            playListMusicInfo.setImage(song.getAlbummid());
            playListMusicInfo.setDuration(0L);
            playListMusicInfo.setUrl(songsData.getStr(song.getSongmid()));
            playListMusicInfo.setPlatform(PlatformEnum.QQ.getCode());
            playListMusicInfo.setPlatformName(PlatformEnum.QQ.getName());
            songs.add(playListMusicInfo);
        }
        return null;
    }

    @Override
    public JSONObject requestResource(JSONObject jsonObject, Users users, UserPlatformBindInfo one) {
        one = getUserPlatformBindInfo(users, one, systemProperties.getMusicUrl()  + "/" + PlatformEnum.NETEASE.getCode());
        TreadLocalUtil.setUserPlatformBindInfoThreadLocal(one);
        return qqClient.requestResource(jsonObject);
    }
}
